<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!-- 部门数据 -->
      <el-form ref="queryForm" prop="categoryId" size="small" inline>
        <el-form-item label="商品分类：">
          <treeselect
                    v-model="queryParams.categoryId"
                    :options="categoryOptions"
                    :show-count="true"
                    placeholder="请选择分类"
                    id="categorySelect"
                    style="width: 180px;"
                  />
        </el-form-item>
        <el-form-item label="商品名称:" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入商品名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-button type="primary" plain icon="el-icon-plus" size="middle" @click="handleAdd" width="80px">添加商品</el-button>
      <el-button type="warning" plain icon="el-icon-download" size="middle" @click="handleExport">导出</el-button>

    </el-row>
    <el-table v-loading="loading"
                  :data="data.commodityList"
                  style="width: 100%"
                  size="mini"
                  :highlight-current-row="true"
                  :header-cell-style=" {fontWeight:'bold'}"
        >
          <el-table-column type="expand">
            <template slot-scope="scope">
              <el-form  label-position="left" inline class="demo-table-expand" >
                <el-row>
                  <el-col :span="8">
                    <el-form-item class="custom-form-item" width="100" label="商品分类：">
                      <span>{{ scope.row.commodityCategory.categoryName }}</span>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item class="custom-form-item" width="100" label="收藏量：">
                      <span>{{ scope.row.collectNumber }}</span>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item class="custom-form-item" width="100" label="虚拟销量：">
                      <span>{{ scope.row.virtualSales }}</span>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row>
                  <el-col :span="8">
                    <el-form-item class="custom-form-item" width="100" label="市场价：">
                      <span>{{ scope.row.marketPrice }}</span>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item class="custom-form-item" width="100" label="成本价：">
                      <span>{{ scope.row.costPrice }}</span>
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-form>
            </template>
          </el-table-column>
          <el-table-column
            prop="commodityId"
            label="ID"
            min-width="50"
          />
          <el-table-column label="商品名称" prop="name" :show-overflow-tooltip="true" min-width="100" />
          <el-table-column label="商品售价" prop="price" min-width="100" align="center"/>
          <el-table-column label="销量" prop="salesNumber" min-width="100" align="center" />
          <el-table-column label="库存" prop="stock" min-width="100" align="center" />
          <el-table-column label="创建时间" align="center" prop="createTime" min-width="150" >
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="状态" prop="status" min-width="100" align="center" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope" >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
              >删除</el-button>
            </template>
          </el-table-column>
      </el-table>
    <pagination
      :total="data.total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-steps :active="currentTab" align-center finish-status="success" append-to-body>
        <el-step title="商品基本信息" />
        <el-step title="商品销售信息" />
      </el-steps>

      <el-form ref="form1" :model="form1" :rules="rules" label-width="100px" >
        <el-row v-show="currentTab === 0" :gutter="24">
          <el-col v-bind="grid2">
            <el-form-item label="商品名称" prop="name">
              <el-input v-model="form1.name" placeholder="请输入商品昵称" maxlength="30" />
            </el-form-item>
          </el-col>
          <el-col v-bind="grid2">
            <el-form-item label="商品分类" prop="categoryId">
              <treeselect v-model="form1.categoryId" :options="categoryOptions" :show-count="true" placeholder="请选择商品分类" maxlength="11" />
            </el-form-item>
          </el-col>
          <el-col v-bind="grid2">
            <el-form-item label="关键字" prop="keywords">
              <el-input v-model="form1.keywords" placeholder="请输入商品关键字" maxlength="11" />
            </el-form-item>
          </el-col>
          <el-col v-bind="grid2">
            <el-form-item label="排序" prop="orderNum">
              <el-input type="number" v-model="form1.orderNum" :min="0" placeholder="请输入排序"  />
            </el-form-item>
          </el-col >
          <el-col v-bind="grid2">
            <el-form-item label="单位" prop="unit">
              <el-input v-model="form1.unit" placeholder="请输入商品单位" maxlength="50" />
            </el-form-item>
          </el-col>
          <el-col v-bind="grid2">
            <el-form-item label="收藏量" prop="score">
              <el-input type="number" v-model="form1.score" :min="0" placeholder="请输入积分"  />
            </el-form-item>
          </el-col >
          <el-col v-bind="grid2">
            <el-form-item label="状态" prop="status">
              <template>
                <el-radio v-model="form1.status" label="启用">启用</el-radio>
                <el-radio v-model="form1.status" label="停用">停用</el-radio>
              </template>
            </el-form-item>
          </el-col >
          <el-col v-bind="grid2">
            <el-form-item  label="商品描述" prop="description">
              <el-input v-model="form1.description" type="textarea" rows="3" placeholder="请输入商品描述" maxlength="250" />
            </el-form-item>
          </el-col >
        </el-row>
        <el-row v-show="currentTab === 1 && !isDisabled">
          <el-col :span="12">
            <el-form-item label="商品售价" prop="price">
              <el-input v-model="form1.price" placeholder="请输入商品售价" maxlength="11" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="成本价" prop="costPrice">
              <el-input type="number" v-model="form1.costPrice" :min="0" placeholder="请输入成本价"  />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="市场价" prop="marketPrice">
              <el-input type="number" v-model="form1.marketPrice" :min="0" placeholder="请输入市场价"  />
            </el-form-item>
          </el-col >
          <el-col :span="12">
            <el-form-item label="销量" prop="salesNumber">
              <el-input v-model="form1.salesNumber" placeholder="请输入销量" maxlength="11" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="虚拟销量" prop="virtualSales">
              <el-input v-model="form1.virtualSales" placeholder="请输入虚拟销量" maxlength="11" />
            </el-form-item>
          </el-col >
          <el-col :span="12">
            <el-form-item label="库存" prop="stock">
              <el-input v-model="form1.stock" placeholder="请输入库存" maxlength="11" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item>
          <el-button v-show="currentTab>0" class="submission priamry_border" @click="handleSubmitUp">上一步</el-button>
          <el-button v-show="currentTab<1" type="primary" class="submission" @click="handleSubmitNest('formValidate')">下一步</el-button>
          <el-button  @click="cancel">取 消</el-button>
          <el-button v-show="currentTab>0" type="primary" class="submission" @click="handleSubmit">提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  addCommodity, CategoryTreeSelect,
  delCommodity,
  getCommodity,
  listCommodity,
  updateCommodity
} from "@/api/commodity/commodity";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  components: {Treeselect},
  data() {
    return {
      data: {
        commodityList: [],
        total: 0
      },
      loading: true,
      showSearch: true,
      menuOptions: [],
      active: 0,
      ids: [],
      title: "",
      open: false,
      defaultProps: {
        children: "children",
        label: "label"
      },
      categoryName: undefined,
      categoryId: undefined,
      currentTab:0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: '',
        categoryId: ''
      },
      grid2: {
        xl: 12,
        lg: 12,
        md: 12,
        sm: 24,
        xs: 24
      },
      // 表单参数
      form1: {
        commodityId: '',
        categoryId: '',
        name: undefined,
        price: '',
        salesNumber: '',
        stock: '',
        keywords: '',
        unit: '',
        description: '',
        status: '',
        orderNum: '',
        score: '',
        costPrice: '',
        marketPrice: '',
        virtualSales: ''
      },
      categoryOptions: [],
      rules: {
        name: [
          {required: true, message: "商品名称不能为空", trigger: "blur"},
          {min: 2, max: 20, message: '用户名称长度必须介于 2 和 20 之间', trigger: 'blur'}
        ],
        categoryId: [
          {required: true, message: "所属分类不能为空", trigger: "blur"}
        ]
      }

    };
  },
  watch: {
    // 根据名称筛选部门树
    categoryName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.getList();
    this.getCategoryTree();
  },
  methods: {
    /** 查询商品列表 */
    getList() {
      this.loading = true;
      listCommodity(this.queryParams).then(response => {
          this.data.commodityList = response.rows;
          this.data.total = response.total;
          this.loading = false;
        }
      );
    },
    /** 查询部门下拉树结构 */
    getCategoryTree() {

      CategoryTreeSelect().then(response => {
        this.categoryOptions = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form1 = {
        name: undefined
      };
      this.resetForm("form1");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    handleChange(categoryId) {
      this.queryParams.pageNum = 1;
      this.queryParams.categoryId = categoryId;
      debugger
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.categoryId = undefined;
      this.queryParams.name = undefined;
      this.$refs.tree.setCurrentKey(null);
      this.handleQuery();
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.open = true;
      this.title = "添加商品";
    },

    /** 提交按钮 */
    handleSubmit: function () {
      this.$refs["form1"].validate(valid => {
        if (this.form1.commodityId !== undefined) {
          updateCommodity(this.form1).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          })
        } else {
          addCommodity(this.form1).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /* 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const commodityId = row.commodityId || this.ids
      getCommodity(commodityId).then(response => {
        this.form1 = response.data;
        this.open = true;
        this.title = "修改商品";
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const commodityIds = row.commodityId || this.ids;
      this.$modal.confirm('是否确认删除商品编号为"' + commodityIds + '"的数据项？').then(function () {
        return delCommodity(commodityIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('commodity/commodity/export', {
        ...this.queryParams
      }, `commodity_${new Date().getTime()}.xlsx`)
    },
    handleSubmitUp(){
      this.currentTab--
    },
    handleSubmitNest(){
      this.currentTab++
    }

  },
}
</script>
